package org.jsbd.boss.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.common.redis.RedisClient;
import org.jsbd.boss.dao.IMiguReadRetentConfigDao;
import org.jsbd.boss.domian.MiguReadRetentConfigBean;
import org.jsbd.boss.service.IMiguReadRetentConfigService;
import org.jsbd.boss.util.CacheKey;
import org.jsbd.boss.util.DateUtil;
import org.jsbd.boss.util.TimeModelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.common.utils.StringUtils;

@Service("miguReadRetentConfigService")
public class MiguReadRetentConfigServiceImpl implements IMiguReadRetentConfigService {

	@Autowired
	private IMiguReadRetentConfigDao miguReadRetentConfigDao;
	@Autowired
	private RedisClient<String,Object> redisClientMigu;

	@Override
	public void saveMiguReadRetentConfig(MiguReadRetentConfigBean miguReadRetentConfig){
		miguReadRetentConfigDao.saveMiguReadRetentConfig(miguReadRetentConfig);
	}

	@Override
	public void updateMiguReadRetentConfig(MiguReadRetentConfigBean miguReadRetentConfig){
		miguReadRetentConfigDao.updateMiguReadRetentConfig(miguReadRetentConfig);
	}

	@Override
	public MiguReadRetentConfigBean getMiguReadRetentConfig(MiguReadRetentConfigBean miguReadRetentConfig) {
		return miguReadRetentConfigDao.getMiguReadRetentConfig(miguReadRetentConfig);
	}

	@Override
	public List<MiguReadRetentConfigBean> getMiguReadRetentConfigList(MiguReadRetentConfigBean miguReadRetentConfig) {
		List<MiguReadRetentConfigBean> configList = miguReadRetentConfigDao.getMiguReadRetentConfigList(miguReadRetentConfig);
		if(configList==null||configList.isEmpty()){
			return new ArrayList<MiguReadRetentConfigBean>();
		}
		
		Map<Object,Object> successMap=redisClientMigu.getHash(CacheKey.MIGUREAD_RENTENT_SUCCESS_NUM_KEY);
		Map<Object,Object> doNumMap=redisClientMigu.getHash(CacheKey.MIGUREAD_RETENT_DO_NUM_HASH_KEY);
		
		String sucessPrefixKey=CacheKey.MIGUREAD_RENTENT_SUCCESS_NUM_KEY;
		String doPrefixKey=CacheKey.MIGUREAD_RETENT_TASK_NUM_DONE;
		String doDay=DateUtil.getCurrentDateStr();
		
		for(MiguReadRetentConfigBean config:configList){
			Integer totalNum=config.getTotalNum();
			if(totalNum==null){
				totalNum=0;
			}
			
			if(config.getRetentRate()==null){
				totalNum=0;
			}else{
				totalNum=totalNum*config.getRetentRate()/100;
			}
			
			String key=sucessPrefixKey+","+config.getChannelId()+","+config.getVersion()+","+doDay+","+config.getRetentDayStr();
			String successNum=(String)successMap.get(key);
			if(StringUtils.isBlank(successNum)){
				successNum="0";
			}
			
			String doKey=doPrefixKey+config.getChannelId()+"_"+config.getVersion()+"_"+doDay+"_"+config.getRetentDayStr();
			Long doNum=(Long)doNumMap.get(doKey);
			if(doNum==null){
				doNum=0L;
			}
			
            int planNum=TimeModelUtils.genMaxNumByTime(Integer.valueOf(totalNum+""), new Date());
			
			String totalNumStr=totalNum+"|"+planNum+"|"+doNum+"|"+successNum;
			config.setTotalNumStr(totalNumStr);
		}
		return configList;
	}

	@Override
	public Map<String, Object> getMiguReadRetentConfigListPage(DataGridModel page, MiguReadRetentConfigBean miguReadRetentConfig) {
		Map<String, Object> results=miguReadRetentConfigDao.getMiguReadRetentConfigListPage(page, miguReadRetentConfig);
		List<MiguReadRetentConfigBean> configList=(List<MiguReadRetentConfigBean>)results.get("rows");
		if(configList==null||configList.isEmpty()){
			return results;
		}
		
		Map<Object,Object> successMap=redisClientMigu.getHash(CacheKey.MIGUREAD_RENTENT_SUCCESS_NUM_KEY);
		Map<Object,Object> doNumMap=redisClientMigu.getHash(CacheKey.MIGUREAD_RETENT_DO_NUM_HASH_KEY);
		
		String sucessPrefixKey=CacheKey.MIGUREAD_RENTENT_SUCCESS_NUM_KEY;
		String doPrefixKey=CacheKey.MIGUREAD_RETENT_TASK_NUM_DONE;
		String doDay=DateUtil.getCurrentDateStr();
		
		for(MiguReadRetentConfigBean config:configList){
			Integer totalNum=config.getTotalNum();
			if(totalNum==null){
				totalNum=0;
			}
			
			if(config.getRetentRate()==null){
				totalNum=0;
			}else{
				totalNum=totalNum*config.getRetentRate()/100;
			}
			
			String key=sucessPrefixKey+","+config.getChannelId()+","+config.getVersion()+","+doDay+","+config.getRetentDayStr();
			String successNum=(String)successMap.get(key);
			if(StringUtils.isBlank(successNum)){
				successNum="0";
			}
			
			String doKey=doPrefixKey+config.getChannelId()+"_"+config.getVersion()+"_"+doDay+"_"+config.getRetentDayStr();
			Long doNum=(Long)doNumMap.get(doKey);
			if(doNum==null){
				doNum=0L;
			}
			
            int planNum=TimeModelUtils.genMaxNumByTime(Integer.valueOf(totalNum+""), new Date());
			
			String totalNumStr=totalNum+"|"+planNum+"|"+doNum+"|"+successNum;
			config.setTotalNumStr(totalNumStr);
		}
		
		return results;
	}

	@Override
	public Integer getMiguReadRetentConfigCount(MiguReadRetentConfigBean miguReadRetentConfig) {
		return miguReadRetentConfigDao.getMiguReadRetentConfigCount(miguReadRetentConfig);
	}
	
	@Override
	public void deleteMiguReadRetentConfig(MiguReadRetentConfigBean miguReadRetentConfig) {
		miguReadRetentConfigDao.deleteMiguReadRetentConfig(miguReadRetentConfig);
	}

	@Override
	public List<MiguReadRetentConfigBean> getByMiguReadId(Integer id) {
		return miguReadRetentConfigDao.selectByMiguReadId(id);
	}

	@Override
	public void copyMiguReadRetentConfig(List<MiguReadRetentConfigBean> targetList) {
		miguReadRetentConfigDao.insertMiguReadRetentConfig(targetList);
	}

	@Override
	@Transactional("transactionManagerCrack")
	public void saveMiguReadRetentConfigNew(List<MiguReadRetentConfigBean> list) {
		miguReadRetentConfigDao.insertMiguReadRetentConfig(list);
	}

	@Override
	public void deleteByMiguReadId(Integer id) {
		miguReadRetentConfigDao.deleteByMiguReadId(id);
	}

	@Override
	public void saveRetent(List<MiguReadRetentConfigBean> mvrcs) {
		miguReadRetentConfigDao.saveRetent(mvrcs);
	}

	@Override
	public void deleteRetentBatch(MiguReadRetentConfigBean mvrcb) {
		miguReadRetentConfigDao.deleteRetentBatch(mvrcb);
	}
	
}
